package festival.game;

import festival.board.Board;
import festival.board.BoardView;
import festival.board.ObservableModel;
import festival.input.InputListener;
import festival.main.MainFrame;
import festival.player.Player;
import festival.player.PlayerPanel;
import festival.player.PlayerView;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GameFrame extends JFrame implements Game.GameStateChangeListener, 
        Game.TurnChangeListener, ObservableModel.ModelChangeListener, ComponentListener {
    
    private Game mGame;
    private GameState mCurrentState;
    
    private PlayerPanel mPlayerPanel;
    private TurnView mTurnView;
    
    public GameFrame(Game game) {
        mGame = game;
        mCurrentState = game.getGameState();
        mGame.setGameStateChangeListener(this);
        mGame.setTurnChangeListener(this);
        mGame.addModelChangeListener(this);
        
        this.onGameStateChange(mCurrentState);
        
        addComponentListener(this);
        
        init();
    }
    
    private void init() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());
        
        mPlayerPanel = new PlayerPanel(mGame.getPlayers());
//        JPanel playerPanel = new JPanel();
//        mPlayerPanel.setLayout(new GridLayout(4, 1));
//        for(Player p : mGame.getPlayers()) {
//            mPlayerPanel.add(new PlayerView(p));
//        }
        mPlayerPanel.setSelectedPlayer(0);
        
        mTurnView = new TurnView(mGame);
        
        GridBagConstraints c = new GridBagConstraints();
        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new GridBagLayout());
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.gridheight = 1;
        leftPanel.add(mTurnView, c);
        
        c.weightx = 1.0;
        c.weighty = 8.0;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.gridheight = 1;
        leftPanel.add(mPlayerPanel, c);
        
        Board board = mGame.getBoard();
        BoardView boardView = new BoardView(board);
        board.addModelChangeListener(boardView);
        
        
        Container pane = getContentPane();
        
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridwidth = 1;
        c.gridheight = 1;
        pane.add(leftPanel, c);
        
        c.weightx = 4.0;
        c.weighty = 1.0;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.gridheight = GridBagConstraints.REMAINDER;
        pane.add(boardView, c);
//        
//        c.weightx = 1.0;
//        c.weighty = 4.0;
//        c.gridwidth = 1;
//        c.gridheight = 1;
//        pane.add(mPlayerPanel, c);
    }
    
    public void paint(Graphics g) {
        super.paint(g);
    }

    public void onGameStateChange(GameState state) {
        for(InputListener l : mCurrentState.getInputListeners()) {
            this.removeKeyListener(l);
        }
        for(InputListener l : state.getInputListeners()) {
            this.addKeyListener(l);
        }
        mCurrentState = state;
        repaint();
    }

    public void onTurnChange(int newPlayerIndex) {
        mPlayerPanel.setSelectedPlayer(newPlayerIndex);
        mTurnView.repaint();
        repaint();
    }

    @Override
    public void onModelChange(ObservableModel model) {
        repaint();
    }
    
    public void componentHidden(ComponentEvent arg0) {
    }

    public void componentMoved(ComponentEvent arg0) {
    }

    public void componentResized(ComponentEvent arg0) {
        repaint();
    }

    public void componentShown(ComponentEvent arg0) {
        repaint();
    }
}
